﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;
using Kpmg.CompanyInfo;

public partial class wuc_ServiceTemplate_Readonly : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //this.hfServiceType.Value = "02.01";
            //this.hfVersionNo.Value = "45";
            //BindData(this.hfServiceType.Value, this.hfVersionNo.Value);
            //this.hfChargeableCode.Value = "30023239";
        }
    }

    public void BindData(string ticketNo)
    {
        //Ticket ticket = TicketOM.GetTicket(ticketNo);
        //this.hfTicketNo.Value = ticketNo;
        //this.hfServiceType.Value = ticket.TypeCode;
        //this.hfVersionNo.Value = ticket.VerNo.ToString();
        //this.hfChargeableCode.Value = ticket.ChargeableCode;

        //IList<ServiceStep> steps = ServiceTemplateOM.ListServiceSteps(ticket.TypeCode, ticket.VerNo);
        //IList<TicketData> datas = TicketOM.ListTicketData(ticketNo);
        ////ViewState["datas"] = datas;
        //this.rptMain.DataSource = steps;
        //this.rptMain.DataBind();
    }

    protected void BindData(string sevType, string verNo)
    {
        //this.rptMain.DataSource = ServiceTemplateOM.ListServiceSteps(sevType, Convert.ToInt32(verNo));
        //this.rptMain.DataBind();
    }

    protected TicketData GetTicketData(string quesCode)
    {
        //return TicketOM.GetTicketData(this.hfTicketNo.Value, quesCode);

        //IList<TicketData> datas = ViewState["datas"] as List<TicketData>;
        //if (datas != null)
        //    return datas.FirstOrDefault(x => x.QuesCode == quesCode);
        return null;
    }

    protected void rptMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ServiceStep sevStep = e.Item.DataItem as ServiceStep;

            if (sevStep != null)
            {
                ((HiddenField)e.Item.FindControl("hfStepNo")).Value = sevStep.StepCode;
                ((Label)e.Item.FindControl("lblHeading")).Text = sevStep.StepCodeUser;
                ((LinkButton)e.Item.FindControl("btnExpand")).Text = sevStep.Description;
            }
        }
    }

    protected void rptChild_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ServiceQuestion sevQue = e.Item.DataItem as ServiceQuestion;

            if (sevQue != null)
            {
                ((Label)e.Item.FindControl("lblTitle")).Text = sevQue.Description;
                ((HiddenField)e.Item.FindControl("hfStepNo")).Value = sevQue.QuesCode;

                CheckBoxList lstCheckBox = ((CheckBoxList)e.Item.FindControl("lstCheckbox"));
                RadioButtonList lstRadioButton = ((RadioButtonList)e.Item.FindControl("lstRadioButton"));
                Label lblElse = ((Label)e.Item.FindControl("lblElse"));

                TicketData td = GetTicketData(sevQue.QuesCode);
                string data = td != null ? td.Answer : "";
                //string data = "abc";
                IList<string> sdata = null;
                switch (sevQue.DataType)
                {
                    case ServiceQuestionDataType.List:
                        lstRadioButton.Items.Clear();
                        sdata = data.Split(new string[] { AppConfigurer.TicketDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var ans in sevQue.Answers)
                        {
                            if (sdata.Contains(ans.Key))
                            {
                                ListItem item = new ListItem()
                                {
                                    Text = ans.Value,
                                    Value = ans.Key,
                                    Selected = true
                                };
                                lstRadioButton.Items.Add(item);
                            }
                        }
                        lstRadioButton.Visible = true;
                        break;

                    case ServiceQuestionDataType.Office:

                        break;

                    case ServiceQuestionDataType.EngTeam:
                        lstCheckBox.Items.Clear();
                        sdata = data.Split(new string[] { AppConfigurer.TicketDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
                        if (!String.IsNullOrEmpty(this.hfChargeableCode.Value))
                        {
                            Chargeable chargeable = ChargeableOM.GetChargeable(this.hfChargeableCode.Value, this.hfProfileCode.Value);
                            if (chargeable != null)
                            {
                                foreach (var obj in chargeable.Members)
                                {
                                    if (sdata.Contains(obj.Key))
                                    {
                                        var staff = StaffDAO.GetObject(obj.Key);
                                        ListItem item = new ListItem()
                                        {
                                            Text = staff.DisplayName,
                                            Value = staff.StaffNo,
                                            Selected = true
                                        };
                                        lstCheckBox.Items.Add(item);
                                    }
                                }
                            }
                        }
                        if (lstCheckBox.Items.Count == 0)
                        {
                            lblElse.Text = "---The engagement team has no member/the chargeable code is not set---";
                            lblElse.Visible = true;
                        }
                        else
                        {
                            lstCheckBox.Visible = true;
                        }
                        break;

                    case ServiceQuestionDataType.CheckBox:
                        lstCheckBox.Items.Clear();
                        sdata = data.Split(new string[] { AppConfigurer.TicketDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var ans in sevQue.Answers)
                        {
                            if (sdata.Contains(ans.Key))
                            {
                                ListItem item = new ListItem()
                                {
                                    Text = ans.Value,
                                    Value = ans.Key,
                                    Selected = true
                                };
                                lstCheckBox.Items.Add(item);
                            }
                        }
                        lstCheckBox.Visible = true;
                        break;

                    case ServiceQuestionDataType.Date:
                    case ServiceQuestionDataType.String:
                    case ServiceQuestionDataType.ConfirmTemplate:
                        TextBox txtBox = (TextBox)e.Item.FindControl("txtbox");
                        txtBox.Text = data;
                        txtBox.Visible = true;
                        break;

                    case ServiceQuestionDataType.File:
                        lblElse.Text = "---Please provide supporting documents---";
                        lblElse.Visible = true;
                        break;

                    case ServiceQuestionDataType.NA:
                    default:
                        break;
                }
            }
        }
    }

    protected void rptMain_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string stepNoVer = ((HiddenField)e.Item.FindControl("hfStepNo")).Value;
            //string[] args = stepNoVer.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);

            Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
            LinkButton btnExpand = (LinkButton)e.Item.FindControl("btnExpand");

            switch (e.CommandName)
            {
                case "FirstExpand":
                    //rptChild.DataSource = ServiceTemplateOM.ListServiceQuestions(stepNoVer, Convert.ToInt32(this.hfVersionNo.Value));
                    //rptChild.DataBind();
                    btnExpand.CommandName = "Collapse";
                    rptChild.Visible = true;
                    break;

                case "Expand":
                    btnExpand.CommandName = "Collapse";
                    rptChild.Visible = true;
                    break;

                case "Collapse":
                    btnExpand.CommandName = "Expand";
                    rptChild.Visible = false;
                    break;
            }
        }
    }

    protected void rptChild_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string stepNo = ((HiddenField)e.Item.FindControl("hfStepNo")).Value;

            Label subElse = (Label)e.Item.FindControl("subElse");
            CheckBoxList subCheckBox = (CheckBoxList)e.Item.FindControl("subCheckBox");

            //IList<WorkflowTemplate> templates = DataSource.Where(x => x.StepNo.StartsWith(stepNo) && x.Seq == 6).ToList();

            //if (templates.Count() > 0)
            //{
            //    subElse.Text = templates.First().Description;
            //    subCheckBox.DataSource = DataSource.Where(x => x.StepNo.StartsWith(stepNo) && x.Seq == 7).ToList();
            //    subCheckBox.DataBind();
            //    subElse.Visible = true;
            //    subCheckBox.Visible = true;
            //}
        }
    }

    protected void lstRadioButton_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}